package com.shaco.heroproject.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.shaco.heroproject.R;
import com.shaco.heroproject.adapter.HeroInforVideoAdapter;
import com.shaco.heroproject.entity.HeroVideoResult;
import com.shaco.heroproject.entity.Result_Video;
import com.shaco.heroproject.volley.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/11/4.
 */
public class HeroInforVideoActivity extends Activity implements View.OnClickListener {
    private RecyclerView recyclerView;
    private HeroInforVideoAdapter heroInforVideoAdapter;
    private VolleySingleton queue;
    private String id, vids;
    private Button activity_hero_video_backBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hero_infor_video);
        initView();
        initData();
    }

    private void initData() {
        id = getIntent().getStringExtra("id");
        vids = getIntent().getStringExtra("vids");
        // 初始化请求队列
        queue = VolleySingleton.getVolleyIntance();
        String url = "http://lol.data.shiwan.com/getHeroVideo/?id=" + id + "&vids=" + vids;
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        HeroVideoResult hr = gson.fromJson(response,
                                HeroVideoResult.class);
                        if (hr != null) {
                            List<Result_Video> result = hr.getResult();
                            heroInforVideoAdapter.addData(result);
                        }
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                // 打印错误信息
                error.printStackTrace();
            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // 添加请求头 在请求头中增加属性
                Map<String, String> map = new HashMap<String, String>();
                // 在请求头中添加apikey
                map.put("Accept-Encoding", "");
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(
                    NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }

        };
        queue.addRequest(request);


    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.heroInfor_videoRecyclerView);
        activity_hero_video_backBtn= (Button) findViewById(R.id.activity_hero_video_backBtn);
        activity_hero_video_backBtn.setOnClickListener(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);

        recyclerView.setLayoutManager(gridLayoutManager);
        // 初始化适配器对象
        heroInforVideoAdapter = new HeroInforVideoAdapter(this);
        // 绑定适配器
        recyclerView.setAdapter(heroInforVideoAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.activity_hero_video_backBtn:
                finish();
                break;

        }
    }
}
